package com.liuqi.loggers.core.converters;

import com.liuqi.loggers.core.Converter;
import com.liuqi.loggers.core.LoggerEvent;

/**
 * converter logger level
 *
 * @author liuqi
 * @date 2024/11/5 16:53
 **/
public class LevelConverter implements Converter {

    private static final String C_T_S = ANSIConstants.ESC_START + ANSIConstants.YELLOW_FG + ANSIConstants.ESC_END;
    private static final String C_D_S = ANSIConstants.ESC_START + ANSIConstants.GREEN_FG + ANSIConstants.ESC_END;
    private static final String C_I_S = ANSIConstants.ESC_START + ANSIConstants.BLUE_FG + ANSIConstants.ESC_END;
    private static final String C_W_S = ANSIConstants.ESC_START + ANSIConstants.RED_FG + ANSIConstants.ESC_END;
    private static final String C_R_S = ANSIConstants.ESC_START + ANSIConstants.BOLD + "91" + ANSIConstants.ESC_END;
    private static final String C_E   = ANSIConstants.ESC_START + ANSIConstants.ESC_END;

    @Override
    public String convert(LoggerEvent event) {
        String cs = "";
        switch (event.getLevel()) {
            case TRACE:
                cs = C_T_S; break;
            case DEBUG:
                cs = C_D_S; break;
            case INFO:
                cs = C_I_S; break;
            case WARN:
                cs = C_W_S; break;
            case ERROR:
                cs = C_R_S; break;
        }
        return cs + event.getLevel().name() + C_E;
    }

    @Override
    public String convert0(LoggerEvent event) {
        return event.getLevel().name();
    }

    @Override
    public String name() {
        return "level";
    }

}
